#include "PlatformProperty.h"

static PropertyStatus getPressStatus(int32_t timeout) {
    auto cc19 = SIGNAL.getcarconfig19();
    auto carmod = SIGNAL.getVehModMngtGlbSafe1CarModSts1();
    auto usgmod = SIGNAL.getVehModMngtGlbSafe1UsgModSts();
    VHALOG.d(
        "SENSOR_FUNC_TIRE_TEMPERATURE status Change cc19[%d] carmod[%d], usgmod[%d], "
        "timeout[%d]",
        cc19, carmod, usgmod, timeout);
    if (!SIGNAL.carConfigIs(SignalRX::carconfig19, sdb::CarConfig19::_01)) {
        if (SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModCnvinc, sdb::UsgModSts1::UsgModActv,
                             sdb::UsgModSts1::UsgModDrvg) &&
            SIGNAL.carModeIs(sdb::CarModSts1::CarModNorm)) {
            if (timeout == static_cast<int32_t>(sdb::Timeout::Timeout)) {
                VHALOG.d("TIRE_PRESSURE PropertyStatus Error");
                return PropertyStatus::Error;
            }
            VHALOG.d("TIRE_PRESSURE PropertyStatus Active");
            return PropertyStatus::Active;
        }
        VHALOG.d("TIRE_PRESSURE PropertyStatus NotActive");
        return PropertyStatus::NotActive;
    }
    VHALOG.d("TIRE_PRESSURE PropertyStatus NotAvailable");
    return PropertyStatus::NotAvailable;
}
// 胎压
SYSTEM_READ_PROPERTY(TIRE_PRESSURE, float) {
    builder.area(VehicleAreaWheel::LEFT_FRONT)
        .initValue([]() { return 0.0f; })
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& last) {
                int32_t timeout = static_cast<int32_t>(SIGNAL.getLeFrntTireMsgPTimeout());
                VHALOG.d("TIRE_PRESSURE status LeFrntTireMsgPTimeout:[%d]", timeout);
                return getPressStatus(timeout);
            },
            SignalRX::carconfig19, SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1, SignalRX::LeFrntTireMsgPTimeout)
        .withValueBySignals(
            []([[maybe_unused]] const float& last) {
                float tirePress = SIGNAL.getLeFrntTireMsgP();
                VHALOG.d("TIRE_PRESSURE Value LeFrntTireMsgP:[%f]", tirePress);
                return tirePress;
            },
            SignalRX::LeFrntTireMsgP)
        .area(VehicleAreaWheel::RIGHT_FRONT)
        .initValue([]() { return 0.0f; })
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& last) {
                int32_t timeout = static_cast<int32_t>(SIGNAL.getRiFrntTireMsgPTimeout());
                VHALOG.d("TIRE_PRESSURE status RiFrntTireMsgPTimeout:[%d]", timeout);
                return getPressStatus(timeout);
            },
            SignalRX::carconfig19, SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1, SignalRX::RiFrntTireMsgPTimeout)
        .withValueBySignals(
            []([[maybe_unused]] const float& last) {
                float tirePress = SIGNAL.getRiFrntTireMsgP();
                VHALOG.d("TIRE_PRESSURE Value RiFrntTireMsgP:[%f]", tirePress);
                return tirePress;
            },
            SignalRX::RiFrntTireMsgP)
        .area(VehicleAreaWheel::LEFT_REAR)
        .initValue([]() { return 0.0f; })
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& last) {
                int32_t timeout = static_cast<int32_t>(SIGNAL.getLeReTireMsgPTimeout());
                VHALOG.d("TIRE_PRESSURE status LeReTireMsgPTimeout:[%d]", timeout);
                return getPressStatus(timeout);
            },
            SignalRX::carconfig19, SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1, SignalRX::LeReTireMsgPTimeout)
        .withValueBySignals(
            []([[maybe_unused]] const float& last) {
                float tirePress = SIGNAL.getLeReTireMsgP();
                VHALOG.d("TIRE_PRESSURE Value LeReTireMsgP:[%f]", tirePress);
                return tirePress;
            },
            SignalRX::LeReTireMsgP)
        .area(VehicleAreaWheel::RIGHT_REAR)
        .initValue([]() { return 0.0f; })
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& last) {
                int32_t timeout = static_cast<int32_t>(SIGNAL.getRiReTireMsgPTimeout());
                VHALOG.d("TIRE_PRESSURE status RiReTireMsgPTimeout:[%d]", timeout);
                return getPressStatus(timeout);
            },
            SignalRX::carconfig19, SignalRX::VehModMngtGlbSafe1UsgModSts,
            SignalRX::VehModMngtGlbSafe1CarModSts1, SignalRX::RiReTireMsgPTimeout)
        .withValueBySignals(
            []([[maybe_unused]] const float& last) {
                float tirePress = SIGNAL.getRiReTireMsgP();
                VHALOG.d("TIRE_PRESSURE Value RiReTireMsgP:[%f]", tirePress);
                return tirePress;
            },
            SignalRX::RiReTireMsgP);
}